/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bdl;

import entities.Team;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author olawale
 */
public class TeamBean {

    private static DataAccess da = DataAccess.getDataAccess();

    public static void createTeam(String teamName) throws Exception{
        Team team = new Team();
        team.setName(teamName);
        try{
            da.em.getTransaction().begin();
            da.em.persist(team);
            da.em.getTransaction().commit();
        }catch(Exception ex){
            if(ex.getCause().toString().contains("MySQLIntegrityConstraintViolationException")){
                throw new Exception("team already exist");
            }
        }
    }

    public static void removeTeam(Team team){
        try{
            da.em.getTransaction().begin();
            da.em.remove(team);
            da.em.getTransaction().commit();
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }

    public static List<Team> getAllTeams(){
        Query query = da.em.createNamedQuery("Team.findAll");

        try{
            List<Team> list = query.getResultList();
            return list;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }
}
